#Boa:Dialog:Dialog1

import wx
import wx.richtext
import utiles

def create(parent):
    return Dialog1(parent)

[wxID_DIALOG1, wxID_DIALOG1BUTTON1, wxID_DIALOG1BUTTON10, 
 wxID_DIALOG1BUTTON11, wxID_DIALOG1BUTTON12, wxID_DIALOG1BUTTON13, 
 wxID_DIALOG1BUTTON14, wxID_DIALOG1BUTTON15, wxID_DIALOG1BUTTON16, 
 wxID_DIALOG1BUTTON17, wxID_DIALOG1BUTTON18, wxID_DIALOG1BUTTON2, 
 wxID_DIALOG1BUTTON3, wxID_DIALOG1BUTTON4, wxID_DIALOG1BUTTON5, 
 wxID_DIALOG1BUTTON6, wxID_DIALOG1BUTTON7, wxID_DIALOG1BUTTON8, 
 wxID_DIALOG1BUTTON9, wxID_DIALOG1RICHTEXTCTRL1, wxID_DIALOG1STATICTEXT1, 
 wxID_DIALOG1STATICTEXT2, wxID_DIALOG1STATICTEXT3, wxID_DIALOG1STATICTEXT5, 
 wxID_DIALOG1STATICTEXT6, wxID_DIALOG1TEXTCTRL1, 
] = [wx.NewId() for _init_ctrls in range(26)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(378, 127), size=wx.Size(479, 493),
              style=wx.DEFAULT_DIALOG_STYLE,
              title='Introducir informacion extra')
        self.SetClientSize(wx.Size(471, 466))

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label='Subtitulo (aparecera subrayado y en negrita una vez subido en la web)',
              name='staticText1', parent=self, pos=wx.Point(16, 32),
              size=wx.Size(341, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2,
              label='Texto', name='staticText2', parent=self, pos=wx.Point(16,
              72), size=wx.Size(28, 13), style=0)

        self.staticText3 = wx.StaticText(id=wxID_DIALOG1STATICTEXT3,
              label='El texto aqui introducido sera agregado a la descripcion de todos los articulos.',
              name='staticText3', parent=self, pos=wx.Point(16, 8),
              size=wx.Size(438, 13), style=0)
        self.staticText3.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.staticText5 = wx.StaticText(id=wxID_DIALOG1STATICTEXT5, label='',
              name='staticText5', parent=self, pos=wx.Point(496, 88),
              size=wx.Size(0, 13), style=0)

        self.staticText6 = wx.StaticText(id=wxID_DIALOG1STATICTEXT6,
              label='codg. HTML', name='staticText6', parent=self,
              pos=wx.Point(400, 85), size=wx.Size(64, 13), style=0)
        self.staticText6.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              'Tahoma'))

        self.textCtrl1 = wx.TextCtrl(id=wxID_DIALOG1TEXTCTRL1, name='textCtrl1',
              parent=self, pos=wx.Point(16, 48), size=wx.Size(376, 21), style=0,
              value='')

        self.richTextCtrl1 = wx.richtext.RichTextCtrl(id=wxID_DIALOG1RICHTEXTCTRL1,
              parent=self, pos=wx.Point(16, 88), size=wx.Size(376, 336),
              style=wx.richtext.RE_MULTILINE, value='')
        self.richTextCtrl1.SetLabel('text')

        self.button1 = wx.Button(id=wxID_DIALOG1BUTTON1, label='Guardar',
              name='button1', parent=self, pos=wx.Point(16, 440),
              size=wx.Size(64, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnGuardar, id=wxID_DIALOG1BUTTON1)

        self.button4 = wx.Button(id=wxID_DIALOG1BUTTON4, label='a',
              name='button4', parent=self, pos=wx.Point(400, 104),
              size=wx.Size(32, 23), style=0)
        self.button4.Bind(wx.EVT_BUTTON, self.OnAminuscula,
              id=wxID_DIALOG1BUTTON4)

        self.button5 = wx.Button(id=wxID_DIALOG1BUTTON5, label='A',
              name='button5', parent=self, pos=wx.Point(432, 104),
              size=wx.Size(32, 23), style=0)
        self.button5.Bind(wx.EVT_BUTTON, self.OnAmayuscula,
              id=wxID_DIALOG1BUTTON5)

        self.button6 = wx.Button(id=wxID_DIALOG1BUTTON6, label='e',
              name='button6', parent=self, pos=wx.Point(400, 128),
              size=wx.Size(32, 23), style=0)
        self.button6.Bind(wx.EVT_BUTTON, self.OnEminuscula,
              id=wxID_DIALOG1BUTTON6)

        self.button7 = wx.Button(id=wxID_DIALOG1BUTTON7, label='E',
              name='button7', parent=self, pos=wx.Point(432, 128),
              size=wx.Size(32, 23), style=0)
        self.button7.Bind(wx.EVT_BUTTON, self.OnEmayuscula,
              id=wxID_DIALOG1BUTTON7)

        self.button8 = wx.Button(id=wxID_DIALOG1BUTTON8, label='i',
              name='button8', parent=self, pos=wx.Point(400, 152),
              size=wx.Size(32, 23), style=0)
        self.button8.Bind(wx.EVT_BUTTON, self.OnIminuscula,
              id=wxID_DIALOG1BUTTON8)

        self.button9 = wx.Button(id=wxID_DIALOG1BUTTON9, label='I',
              name='button9', parent=self, pos=wx.Point(432, 152),
              size=wx.Size(32, 23), style=0)
        self.button9.Bind(wx.EVT_BUTTON, self.OnImayuscula,
              id=wxID_DIALOG1BUTTON9)

        self.button10 = wx.Button(id=wxID_DIALOG1BUTTON10, label='o',
              name='button10', parent=self, pos=wx.Point(400, 176),
              size=wx.Size(32, 23), style=0)
        self.button10.Bind(wx.EVT_BUTTON, self.OnOminuscula,
              id=wxID_DIALOG1BUTTON10)

        self.button11 = wx.Button(id=wxID_DIALOG1BUTTON11, label='O',
              name='button11', parent=self, pos=wx.Point(432, 176),
              size=wx.Size(32, 23), style=0)
        self.button11.Bind(wx.EVT_BUTTON, self.OnOmayuscula,
              id=wxID_DIALOG1BUTTON11)

        self.button12 = wx.Button(id=wxID_DIALOG1BUTTON12, label='u',
              name='button12', parent=self, pos=wx.Point(400, 200),
              size=wx.Size(32, 23), style=0)
        self.button12.Bind(wx.EVT_BUTTON, self.OnUminuscula,
              id=wxID_DIALOG1BUTTON12)

        self.button13 = wx.Button(id=wxID_DIALOG1BUTTON13, label='U',
              name='button13', parent=self, pos=wx.Point(432, 200),
              size=wx.Size(32, 23), style=0)
        self.button13.Bind(wx.EVT_BUTTON, self.OnUmayuscula,
              id=wxID_DIALOG1BUTTON13)

        self.button14 = wx.Button(id=wxID_DIALOG1BUTTON14, label='n',
              name='button14', parent=self, pos=wx.Point(400, 224),
              size=wx.Size(32, 23), style=0)
        self.button14.Bind(wx.EVT_BUTTON, self.OnNtildeMinuscula,
              id=wxID_DIALOG1BUTTON14)

        self.button15 = wx.Button(id=wxID_DIALOG1BUTTON15, label='N',
              name='button15', parent=self, pos=wx.Point(432, 224),
              size=wx.Size(32, 23), style=0)
        self.button15.Bind(wx.EVT_BUTTON, self.OnNtildeMayuscula,
              id=wxID_DIALOG1BUTTON15)

        self.button3 = wx.Button(id=wxID_DIALOG1BUTTON3, label='Link',
              name='button3', parent=self, pos=wx.Point(400, 256),
              size=wx.Size(64, 23), style=0)
        self.button3.Bind(wx.EVT_BUTTON, self.OnLink, id=wxID_DIALOG1BUTTON3)

        self.button16 = wx.Button(id=wxID_DIALOG1BUTTON16, label='Parrafo',
              name='button16', parent=self, pos=wx.Point(400, 288),
              size=wx.Size(64, 23), style=0)
        self.button16.Bind(wx.EVT_BUTTON, self.OnParrafo,
              id=wxID_DIALOG1BUTTON16)

        self.button17 = wx.Button(id=wxID_DIALOG1BUTTON17, label='Salto Linea',
              name='button17', parent=self, pos=wx.Point(400, 320),
              size=wx.Size(64, 23), style=0)
        self.button17.Bind(wx.EVT_BUTTON, self.OnSaltoLinea,
              id=wxID_DIALOG1BUTTON17)

        self.button18 = wx.Button(id=wxID_DIALOG1BUTTON18, label='Negrita',
              name='button18', parent=self, pos=wx.Point(400, 352),
              size=wx.Size(64, 23), style=0)
        self.button18.Bind(wx.EVT_BUTTON, self.OnNegrita,
              id=wxID_DIALOG1BUTTON18)

        self.button2 = wx.Button(id=wxID_DIALOG1BUTTON2, label='Salir',
              name='button2', parent=self, pos=wx.Point(400, 440),
              size=wx.Size(64, 23), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnSalir, id=wxID_DIALOG1BUTTON2)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.verificaExistenciaTexto()
        
    def verificarArchivosNecesarios(self,archivo):
        import os
        resultado = True
        if not os.path.lexists(archivo):
            resultado = False
        return resultado

    def verificaExistenciaTexto(self):
        archivo = "saskaStoreInfoExtra.txt"
        if not self.verificarArchivosNecesarios(archivo):
            print "Archivo creado"
            f = open(archivo,"w")
            self.textCtrl1.SetFocus()
            return
        else:
            print "Archivo abierto para lectura"
            f=open(archivo,"r")
            lista = f.readlines()
            f.close()
        cadena = ""
        subtitulo = ""
        for i,item in enumerate(lista):
            aux = item
            if i==0:
                aux = aux.replace('&aacute;','a')
                aux = aux.replace('&eacute;','e')
                aux = aux.replace('&iacute;','i')
                aux = aux.replace('&oacute;','o')
                aux = aux.replace('&uacute;','u')
                aux = aux.replace('&Aacute;','A')
                aux = aux.replace('&Eacute;','E')
                aux = aux.replace('&Iacute;','I')
                aux = aux.replace('&Oacute;','O')
                aux = aux.replace('&Uacute;','U')
                aux = aux.replace('&ntilde;','n')
                aux = aux.replace('&Ntilde;','N')
                aux = aux.replace('<a href="http://www.saska.com.ar/ayuda" target="_blank">','')
                aux = aux.replace('</a>','')
                aux = aux.replace('<p>','')
                aux = aux.replace('</p>','')
                aux = aux.replace('<strong>','')
                aux = aux.replace('</strong>','')
                aux = aux.replace('<span style="text-decoration: underline;">','')
                aux = aux.replace('</span>','')
                subtitulo = aux[:-1]
            else:
                cadena+= aux
        self.textCtrl1.SetLabel(subtitulo)
        self.richTextCtrl1.SetValue(cadena)
        self.richTextCtrl1.SetFocus()

    def OnSalir(self, event):
        self.Close()

    def OnLink(self, event):
        import saskaStoreURL
        dlg = saskaStoreURL.Dialog1(self)
        url = ""
        try:
            dlg.ShowModal()
            url = dlg.getURL()
        finally:
            dlg.Destroy()

        if url == "":
            return
        inicio,final = self.richTextCtrl1.GetSelection()
        aux = self.richTextCtrl1.GetValue()
        cadena = aux[:inicio]+'<a href="'+url+'" target="_blank">'
        cadena+= aux[inicio:final]+'</a>'+aux[final:]
        self.richTextCtrl1.SetValue(cadena)

    def insertarLetra(self,letra):
        punto = self.richTextCtrl1.GetInsertionPoint()
        aux = self.richTextCtrl1.GetValue()
        cadena = aux[:punto]+letra+aux[punto:]
        self.richTextCtrl1.SetValue(cadena)
        self.richTextCtrl1.SetInsertionPoint(punto)
        self.richTextCtrl1.SetFocus()

    def OnAminuscula(self, event):
        self.insertarLetra("&aacute;")

    def OnAmayuscula(self, event):
        self.insertarLetra("&Aacute;")

    def OnEminuscula(self, event):
        self.insertarLetra("&eacute;")

    def OnEmayuscula(self, event):
        self.insertarLetra("&Eacute;")

    def OnIminuscula(self, event):
        self.insertarLetra("&iacute;")

    def OnImayuscula(self, event):
        self.insertarLetra("&Iacute;")

    def OnOminuscula(self, event):
        self.insertarLetra("&oacute;")

    def OnOmayuscula(self, event):
        self.insertarLetra("&Oacute;")

    def OnUminuscula(self, event):
        self.insertarLetra("&uacute;")

    def OnUmayuscula(self, event):
        self.insertarLetra("&Uacute;")

    def OnNtildeMinuscula(self, event):
        self.insertarLetra("&nacute;")

    def OnNtildeMayuscula(self, event):
        self.insertarLetra("&Nacute;")

    def OnParrafo(self, event):
        inicio,final = self.richTextCtrl1.GetSelection()
        aux = self.richTextCtrl1.GetValue()
        cadena = aux[:inicio]+'<p>'+aux[inicio:final]+'</p>'+aux[final:]
        self.richTextCtrl1.SetValue(cadena)

    def OnSaltoLinea(self, event):
        self.insertarLetra("<br>")

    def OnNegrita(self, event):
        inicio,final = self.richTextCtrl1.GetSelection()
        aux = self.richTextCtrl1.GetValue()
        cadena = aux[:inicio]+'<strong>'+aux[inicio:final]+'</strong>'+aux[final:]
        self.richTextCtrl1.SetValue(cadena)

    def OnGuardar(self, event):
        aux = self.textCtrl1.GetLabel()
        subtitulo = '<strong><span style="text-decoration: underline;">'
        subtitulo+= aux+'</span></strong>'
        cadena = self.richTextCtrl1.GetValue()
        cadena = subtitulo+"\n"+cadena
        archivo = "saskaStoreInfoExtra.txt"
        f=open(archivo,"w")
        lista = f.write(cadena)
        f.close()
        self.Close()
 